import { h, onMounted, reactive, ref, toRaw } from "vue";
import type { PaginationProps } from "@pureadmin/table";
import type { FormItemProps } from "../utils/types";
import { addDialog } from "@/components/ReDialog";
import { deviceDetection } from "@pureadmin/utils";
import editForm from "../form.vue";
import { message } from "@/utils/message";
import {
  createBulkOrderApi,
  updateBulkOrderApi,
  delBulkOrderApi,
  getBulkOrderShowApi,
  getBulkOrderListApi
} from "@/api/corpApi";
export function useCorps() {
  const form = reactive({
    page: 1,
    perPage: 10,
    keyword: "",
  });
  const basicPar = ref([
	  {
		  id: 1,
		  name: '系统参数'
	  },
	  {
		  id: 2,
		  name: '格口坐标参数表'
	  },
	  {
		  id: 3,
		  name: '服务控制'
	  }
  ])
  const WCSswitch = ref([
	  {
		  id: 1,
		  name: '系统参数',
		  isSwitch: false
	  },
	  {
		  id: 2,
		  name: '格口坐标参数表',
		  isSwitch: true
	  },
	  {
		  id: 3,
		  name: '服务控制',
		  isSwitch: false
	  }
  ])
  const formRef = ref();
  const dataList = ref([]);
  const isShow = ref(false);
  const loading = ref(true);
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 10,
    currentPage: 1,
    background: true
  });
  const indexMethod = (index: number) => {
    return index + 1;
  };
  const getStatusType = (type, text = false) => {
    switch (type) {
      case 0:
        return text ? "未审核" : "danger";
      case 1:
        return text ? "启用" : "primary";
      case 2:
        return text ? "禁用" : "info";
    }
  };
  const columns: TableColumnList = [
    {
      label: "线体-格口",
      prop: "yzjmtnma"
    },
    {
      label: "格口类型",
      prop: "gsjmtd"
    },
    {
      label: "新一代格口",
      prop: "uibpvltd"
    },
	{
	  label: "配发格口",
	  prop: "uibpvltd"
	},
	{
	  label: "装车口",
	  prop: "scmnuijm"
	},
	{
	  label: "已装车重量/上限(千克)",
	  prop: "scmnuijm",
	  minWidth: 120
	}
    // {
    //   label: "操作",
    //   fixed: "right",
    //   slot: "operation"
    // }
  ];
  function handleSizeChange(val: number) {
    form.page = 1;
    form.perPage = val;
    onSearch()
  }

  function handleCurrentChange(val: number) {
    form.page = val;
    onSearch()
  }

  async function onSearch() {
    loading.value = true;
    const { result } = await getBulkOrderListApi(toRaw(form));
    dataList.value = result.list;
    pagination.total = result.total;
    pagination.pageSize = result.per_page;
    pagination.currentPage = result.current_page;

    setTimeout(() => {
      loading.value = false;
    }, 150);
  }

  const resetForm = formEl => {
    if (!formEl) return;
    formEl.resetFields();
    onSearch();
  };
  const formInline = ref();
  async function openDialog(title = "新增", row?: FormItemProps) {
    if (`${title}` == "新增") {
      formInline.value = {
        title: "新增"
      };
    } else {
      const { result } = await getBulkOrderShowApi({ corpId: 0 });
      formInline.value = {
        title: "修改",
        corpId: row?.Id ?? "",
        name: result?.name ?? ""
      };
    }
    addDialog({
      title: `${title}企业信息`,
      props: {
        formInline: formInline
      },
      width: "44%",
      draggable: true,
      fullscreen: deviceDetection(),
      fullscreenIcon: true,
      closeOnClickModal: false,
      contentRenderer: () => h(editForm, { ref: formRef }),
      beforeSure: (done, { options }) => {
        const FormRef = formRef.value.getRef();
        const curData = options.props.formInline as FormItemProps;
        function chores() {
          message(`您${title}了工作计划`, {
            type: "success"
          });
          done();
          onSearch();
        }

        FormRef.validate(valid => {
          if (valid) {
            if (title === "新增") {
              createBulkOrderApi(curData)
                .then(() => {
                  chores();
                })
                .catch(error => {
                  message(error.response.data.msg ?? "操作失败,请联系管理员", {
                    type: "error"
                  });
                });
            } else {
              updateBulkOrderApi(curData)
                .then(() => {
                  chores();
                })
                .catch(error => {
                  message(error.response.data.msg ?? "操作失败,请联系管理员", {
                    type: "error"
                  });
                });
            }
          }
        });
      }
    });
  }
  onMounted(async () => {
    onSearch();
  });

  function handleDelete(row) {
    delBulkOrderApi({ workPlanId: row.workPlanId }).then(() => {
      message(`删除成功`, { type: "success" });
      onSearch();
    });
  }
  function confirmStatus() {
    alert("审核通过");
  }
  function refusedStatus() {
    alert("审核拒绝");
  }
  return {
    form,
    isShow,
    loading,
    columns,
    dataList,
    pagination,
    onSearch,
    resetForm,
    openDialog,
    handleSizeChange,
    handleCurrentChange,
    deviceDetection,
    handleDelete,
    confirmStatus,
    refusedStatus,
	basicPar,
	WCSswitch
  };
}
